<script setup lang="ts">
import { getEmployeeListAPI } from "@/apis/employee";
import {
  createEmployeeAPI,
  delEmployeeAPI,
  getRoleListAPI,
  putParkSysUserAPI,
} from "@/apis/system";
import type { Employee, EmployeeParams } from "@/types/employee";
import type { Role } from "@/types/system";
import { ElMessage, ElMessageBox } from "element-plus";
import { ref } from "vue";
// 员工列表
const employeeList = ref<Employee[]>([]);
// 搜索条件
const formData = ref<EmployeeParams>({
  page: 1,
  pageSize: 5,
  name: "",
});

// 总条数
const total = ref(0);
// 分页
const handleCurrentChange = (page: number) => {
  formData.value.page = page;
  getEmployeeList();
};

// 获取员工列表
const getEmployeeList = async () => {
  const res = await getEmployeeListAPI(formData.value);
  employeeList.value = res.data.rows;
  total.value = res.data.total;
};
getEmployeeList();
// 删除员工
const delEmployee = async (id: string) => {
  ElMessageBox.confirm("确定删除该员工吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await delEmployeeAPI(id);
    ElMessage.success("删除成功");
    getEmployeeList();
  });
};

// 编辑员工
const editEmployee = async (value: Employee) => {
  // console.log('value', value.id, value.name, value.userName, value.phonenumber, value.roleId, value.status)
  await putParkSysUserAPI({
    id: value.id,
    name: value.name,
    userName: value.userName,
    phonenumber: value.phonenumber,
    roleId: value.roleId,
    status: value.status,
  });
  dialogVisible.value = true;
  //数据回显
  addForm.value.name = value.name;
  addForm.value.userName = value.userName;
  addForm.value.phonenumber = value.phonenumber;
  addForm.value.roleId = value.roleId as string;
  addForm.value.status = value.status;
};
// ---------------------------------------弹框---------------------------------------
const dialogVisible = ref(); // 控制弹框打开关闭
const roleList = ref<Role[]>([]); // 角色列表
const addForm = ref({
  name: "",
  phonenumber: "",
  roleId: "",
  status: 1,
  userName: "",
});
// 表单验证
const addFormRules = ref({
  name: [{ required: true, message: "请输入员工姓名", trigger: "blur" }],
  userName: [{ required: true, message: "请输入登录账号", trigger: "blur" }],
  phonenumber: [{ required: true, message: "请输入联系方式", trigger: "blur" }],
  roleId: [{ required: true, message: "请分配角色", trigger: "blur" }],
  status: [{ required: true, message: "请选择员工状态", trigger: "blur" }],
});
//关闭弹窗
const closeDialog = async () => {
  dialogVisible.value = false;
  addForm.value.roleId = "";
};
// 打开弹窗
const addEmployee = async () => {
  dialogVisible.value = true;
  const res = await getRoleListAPI();
  roleList.value = res.data;
};

// 确认添加
const addFormRef = ref();
const confirmAdd = async () => {
  // 添加员工
  await createEmployeeAPI(addForm.value);
  // 关闭弹窗
  dialogVisible.value = false;
  // 刷新员工列表
  getEmployeeList();
  // 提示
  ElMessage.success("添加成功");
  // 重置表单
  addFormRef.value.resetFields();
};
</script>

<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addEmployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ scope.row.status === 0 ? "禁用" : "启用" }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" width="200" />
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="scope">
            <el-button size="small" type="text" @click="editEmployee(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="text"
              @click="delEmployee(scope.row.id)"
              >删除</el-button
            >
            <el-button size="small" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :total="total"
        :page-size="formData.pageSize"
        :current-page="formData.page"
        layout="total, prev, pager, next"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加员工 -->
    <el-dialog
      title="添加员工"
      width="480px"
      v-model="dialogVisible"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addFormRef"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
        >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" maxlength="11" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId!"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="confirmAdd"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
